package com.qiantu.phone.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.SceneManagerBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.PrivateSceneMemberAdapter;

import java.util.List;

import okhttp3.Call;

public class ScenePrivateMemberManagerActivity extends AppActivity {

    private RecyclerView mRecyclerView;
    private PrivateSceneMemberAdapter mPrivateSceneMemberAdapter;
    private SwitchButton clickSwitchButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_private_member_manager;
    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mPrivateSceneMemberAdapter = new PrivateSceneMemberAdapter();
        mPrivateSceneMemberAdapter.addChildClickViewIds(R.id.btn_switch);
        mPrivateSceneMemberAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                clickSwitchButton = (SwitchButton) view;
                clickSwitchButton.setChecked(!clickSwitchButton.isChecked());
                SceneManagerBean sceneManagerBean = mPrivateSceneMemberAdapter.getData().get(position);
                sceneIsDisable(sceneManagerBean.getSceneSerialNo(), clickSwitchButton.isChecked());
            }
        });
        mRecyclerView.setAdapter(mPrivateSceneMemberAdapter);
    }

    @Override
    protected void initData() {
        String mainSceneSerialNo = getString("mainSceneSerialNo");
        LLHttpManager.privateSceneManager(this, mainSceneSerialNo, 4, new HttpCallback<HttpData<List<SceneManagerBean>>>(null){
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<List<SceneManagerBean>> result) {
                List<SceneManagerBean> data = result.getData();
                mPrivateSceneMemberAdapter.setNewInstance(data);
            }

            @Override
            public void onFail(Exception e) {
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void sceneIsDisable(String sceneSerialNo, boolean isDisable){
        LLHttpManager.sceneIsDisable(this, sceneSerialNo, isDisable, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                clickSwitchButton.setChecked(!clickSwitchButton.isChecked());
            }

            @Override
            public void onFail(Exception e) {
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }
}